class Solution(object):
    def minMutation(self, start, end, bank):
        """
        :type start: str
        :type end: str
        :type bank: List[str]
        :rtype: int
        """
        n = len(bank)
        used_list = [0] * n
        now_set = {start}

        def judge(s1, s2):
            count = 0
            for i in range(8):
                if s1[i] == s2[i]:
                    count += 1
            if count == 7:
                return True

        steps = 0
        while now_set:
            next_set = set()
            steps += 1
            for s in now_set:
                for i in range(n):
                    if used_list[i] == 0 and judge(s, bank[i]):
                        if bank[i] == end:
                            return steps
                        next_set.add(bank[i])
                        used_list[i] = 1
            now_set = next_set
        return -1




data = Solution()
start = "AAAAACCC"
end = "AACCCCCC"
bank = ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
print(data.minMutation(start, end, bank))
